package com.ehouse;

import java.io.IOException;
import java.util.List;

public class RulesBL {
	private static RulesBL instance;

	private RulesBL() {

	}

	public static RulesBL getInstance() {
		if (instance == null)
			instance = new RulesBL();

		return instance;

	}

	public void setLightRules(String rules) throws IOException {
		if (!"".equals(rules)) {
			Rule lightrule;

			String[] split = rules.split("\\|");
			for (int x = 0; x < split.length; x++) {
				String rule = split[x];
				if ("".equals(rule)) {
					continue;
				}
				String[] rule_parts = rule.split("\\\\");
				String id = rule_parts[0];

				String time = rule_parts[1];
				String[] rooms = rule_parts[2].split("/");
				boolean[] states = new boolean[5];
				int j = 0;
				for (int i = 0; i < rooms.length; i = i + 2) {
					states[j] = rooms[i + 1].equals("TRUE");
					j++;
				}
				states[4] = rule_parts[3].equals("TRUE");

				lightrule = new Rule(id, states, time);

				Drivers.getInstance().addLightRule(lightrule);
			}
		}

	}

	public void setIrrigationRules(String rules) throws IOException {
		if (!"".equals(rules)) {
			Rule irrigationRule;

			for (String rule : rules.split("\\|")) {
				String[] rule_parts = rule.split("\\\\");
				String id = rule_parts[0];
				String time = rule_parts[1];
				boolean state = rule_parts[2].equals("TRUE");

				irrigationRule = new Rule(id, state, time);

				Drivers.getInstance().addIrrigationRule(irrigationRule);
			}
		}
	}

	public void setTempRule(String rule) throws IOException {
		if (!"".equals(rule)) {
			String[] rule_parts = rule.split("\\\\");
			String id = rule_parts[0];
			int desiredTemp = Integer.parseInt(rule_parts[1]);

			Rule r = new Rule(id, desiredTemp, null);

			Drivers.getInstance().addTempRule(r);
		}
	}

	public String getLightRules() throws IOException {
		List<String> rules = Drivers.getInstance().getLightRules();
		String rules_string = "";

		for (String rule : rules) {
			if (!"".equals(rule)) {
				rules_string += rule;
				rules_string += "|";
			}
		}

		if (rules_string.length() != 0) {
			rules_string = rules_string.substring(0, rules_string.length() - 1);
		}
		return rules_string;
	}

	public String getIrrigationRules() throws IOException {
		List<String> rules = Drivers.getInstance().getIrrigationRules();
		String rules_string = "";

		for (String rule : rules) {
			if (!"".equals(rule)) {
				rules_string += rule;
				rules_string += "|";
			}
		}

		if (rules_string.length() != 0) {
			rules_string = rules_string.substring(0, rules_string.length() - 1);
		}
		return rules_string;
	}

	public String getTempRule() throws IOException {
		List<String> rules = Drivers.getInstance().getTempRule();
		String rules_string = "";

		for (String rule : rules) {
			if (!"".equals(rule))
				rules_string += rule;
		}

		return rules_string;
	}

	public void deleteLightRule(String guid) throws IOException {
		Drivers.getInstance().deleteLightRule(guid);
	}

	public void deleteIrrigationRule(String guid) throws IOException {
		Drivers.getInstance().deleteIrrigationRule(guid);
	}

	public void deleteTempRule() throws IOException {
		Drivers.getInstance().deleteTempRule();
	}
}